package com.yex.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.base.WalletOper;
import com.yex.base.WalletOperEnum;
import com.yex.base.ZeroResultException;
import com.yex.common.utils.DateUtil;
import com.yex.common.utils.PageUtils;
import com.yex.dao.RedemptionDao;
import com.yex.entity.RedemptionEntity;
import com.yex.entity.RedemptionLogEntity;
import com.yex.entity.WalletEntity;
import com.yex.service.LocaleMessageSourceService;
import com.yex.service.RedemptionLogService;
import com.yex.service.RedemptionService;
import com.yex.service.WalletService;
import com.yex.utils.JsonResultVo;
import com.yex.utils.MathUtil;
import com.yex.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("redemptionService")
public class RedemptionServiceImpl extends ServiceImpl<RedemptionDao, RedemptionEntity> implements RedemptionService {
    @Autowired
    private LocaleMessageSourceService message;

    @Autowired
    private RedemptionLogService redemptionLogService;

    @Autowired
    private WalletService walletService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<RedemptionEntity> page = this.selectPage(
                new Query<RedemptionEntity>(params).getPage(),
                new EntityWrapper<RedemptionEntity>()
        );
        return new PageUtils(page);
    }


    @Override
    @Transactional
    public Integer updateRedemption() {
        List<RedemptionEntity> redemptionEntityList = baseMapper.selectRedemptionList();
        for (RedemptionEntity redemptionEntity : redemptionEntityList) {

            RedemptionLogEntity redemptionLogEntity = redemptionLogService.selectByRedemptionId(redemptionEntity.getId(), DateUtil.format(DateUtil.ZH_CN_DATE));
            if (redemptionLogEntity != null) {
                continue;
            }

            BigDecimal total = MathUtil.mul(redemptionEntity.getTotal(), MathUtil.div(redemptionEntity.getRate(), new BigDecimal("100")));
            if (MathUtil.add(redemptionEntity.getSuccessAmount(), total).compareTo(redemptionEntity.getTotal()) == 0) {
                Integer result = baseMapper.updateSuccessAmountStatus(total, redemptionEntity.getId());
                if (result <= 0) {
                    throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
                }
            }else {
                Integer result = baseMapper.updateSuccessAmount(total, redemptionEntity.getId());
                if (result <= 0) {
                    throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
                }
            }

            RedemptionLogEntity redemptionLog = new RedemptionLogEntity();
            redemptionLog.setUserId(redemptionEntity.getUserId());
            redemptionLog.setRedemptionId(redemptionEntity.getId());
            redemptionLog.setCreateTime(new Date());
            redemptionLog.setTime(DateUtil.format(DateUtil.ZH_CN_DATE));
            redemptionLog.setStatus(1);
            redemptionLog.setTotal(total);
            if (!redemptionLogService.insert(redemptionLog)) {
                throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
            }
            WalletEntity walletEntity = walletService.selectByUserId(redemptionEntity.getUserId());
            WalletOper operEtcc = new WalletOper();
            operEtcc.setUserId(redemptionEntity.getUserId());
            operEtcc.setAmount(total);
            operEtcc.setRemark("解冻锁仓");
            operEtcc.setOper(WalletOperEnum.UNFRONZEN);
            operEtcc.setId(walletEntity.getId());
            walletService.updateWallet(operEtcc);
        }

        return 1;
    }
}
